<template>
  <Dialog v-model="dialogVisible" :title="t('batch.operate.setupStage')" width="1400">
    <ContentWrap>
      <el-button
        type="primary"
        plain
        @click="openForm('create')"
        v-hasPermi="['recruit:batch:create']"
      >
        <Icon icon="ep:plus" class="mr-5px" />
        {{ t('action.add') }}
      </el-button>
    </ContentWrap>

    <!-- 列表 -->
    <ContentWrap>
      <vxe-table
        id="iBatchStage-table"
        v-loading="loading"
        :data="list"
        stripe
        :custom-config="vxeTableConfig"
        :column-config="{ resizable: true }"
        :size="vxeTableSize"
      >
        <vxe-column :title="t('common.index')" type="seq" field="seq" width="60" align="center" />
        <vxe-column
          :title="t('batchStage.fields.stageName')"
          field="stageName"
          align="center"
          show-header-overflow
          show-overflow
          min-width="100"
        />
        <vxe-column
          :title="t('batchStage.fields.recruitAmount')"
          field="recruitAmount"
          align="center"
          show-header-overflow
          show-overflow
          min-width="100"
        />
        <vxe-column
          :title="t('batchStage.fields.recruitStartTime')"
          field="recruitStartTime"
          align="center"
          show-header-overflow
          show-overflow
          min-width="100"
          :formatter="vxeDateFormatter"
        />
        <vxe-column
          :title="t('batchStage.fields.recruitEndTime')"
          field="recruitEndTime"
          align="center"
          show-header-overflow
          show-overflow
          min-width="100"
          :formatter="vxeDateFormatter"
        />
        <vxe-column
          :title="t('batchStage.fields.restrictionType')"
          align="center"
          show-header-overflow
          show-overflow
          min-width="100"
        >
          <template #default="scope">
            <p v-if="scope.row.summerEnable">{{ t('student.index.typeSummerCamp') }}</p>
            <p v-if="scope.row.recommendEnable">{{ t('student.index.typeRecommended') }}</p>
            <p v-if="scope.row.unifiedEnable">{{ t('student.index.typeUnifiedRetest') }}</p>
          </template>
        </vxe-column>
        <vxe-column
          :title="t('common.operate')"
          field="operator"
          align="center"
          fixed="right"
          width="140"
        >
          <template #default="scope">
            <el-button
              link
              type="primary"
              @click="openForm('update', scope.row.id)"
              v-hasPermi="['recruit:batch:update']"
            >
              {{ t('action.edit') }}
            </el-button>
            <el-button
              link
              type="danger"
              @click="handleDelete(scope.row.id)"
              v-hasPermi="['recruit:batch:delete']"
            >
              {{ t('action.delete') }}
            </el-button>
          </template>
        </vxe-column>
      </vxe-table>
      <!-- 分页 -->
      <Pagination
        :total="total"
        v-model:page="queryParams.pageNo"
        v-model:limit="queryParams.pageSize"
        @pagination="getList"
      />
    </ContentWrap>

    <!-- 表单弹窗：添加/修改 -->
    <BatchStageForm ref="formRef" @success="getList" />
  </Dialog>
</template>

<script setup lang="ts">
import { vxeDateFormatter } from '@/utils/formatTime'
import { BatchStageApi, BatchStageVO } from '@/api/recruit/batchstage'
import BatchStageForm from './BatchStageForm.vue'
import type { VxeTableInstance, VxeTablePropTypes, VxeToolbarInstance } from 'vxe-table'
import { useAppStore } from '@/store/modules/app'

/** 招生阶段 列表 */
defineOptions({ name: 'BatchStage' })

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化

const dialogVisible = ref(false)
const loading = ref(true) // 列表的加载中
const list = ref<BatchStageVO[]>([]) // 列表的数据
const total = ref(0) // 列表的总页数
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  stageName: undefined,
  batchId: undefined,
  recruitAmount: undefined,
  recruitStartTime: [],
  recruitEndTime: [],
  summerEnable: undefined,
  recommendEnable: undefined,
  unifiedEnable: undefined,
  remark: undefined,
  createTime: []
})
const queryFormRef = ref() // 搜索的表单
const exportLoading = ref(false) // 导出的加载中

const vxeTableConfig: VxeTablePropTypes.CustomConfig = {
  storage: true,
  mode: 'drawer',
  checkMethod({ column }) {
    return !['seq', 'operator'].includes(column.field)
  }
}

const appStore = useAppStore()
const vxeTableSize = computed(() => appStore.getCurrentSizeVxe)

/** 查询列表 */
const getList = async () => {
  queryParams.batchId = batchId.value as any
  loading.value = true
  try {
    const data = await BatchStageApi.getBatchStagePage(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

/** 添加/修改操作 */
const formRef = ref<InstanceType<typeof BatchStageForm>>()
const openForm = (type: string, id?: number) => {
  if (batchId.value) {
    formRef.value?.open(batchId.value, type, id)
  }
}

/** 删除按钮操作 */
const handleDelete = async (id: number) => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    // 发起删除
    await BatchStageApi.deleteBatchStage(id)
    message.success(t('common.delSuccess'))
    // 刷新列表
    await getList()
  } catch {}
}

const batchId = ref('')
const open = (id: string) => {
  batchId.value = id
  list.value = []
  total.value = 0
  dialogVisible.value = true
  getList()
}

/** 初始化 **/
onMounted(() => {
  getList()
})

defineExpose({
  open
})
</script>
